package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxReturnList;
import com.adk.backend.service.ReturnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退货管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/return")
public class ReturnController {
    
    @Autowired
    private ReturnService returnService;
    
    /**
     * 分页查询退货列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<Map<String, Object>>> getReturnList(
            @RequestParam(required = false) Integer orderType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String clientName,
            @RequestParam(required = false) String ibDate,
            @RequestParam(required = false) String trackingNo,
            @RequestParam(required = false) List<String> trackingNos,
            @RequestParam(required = false) Long customerId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("orderType", orderType);
        params.put("status", status);
        params.put("clientName", clientName);
        params.put("ibDate", ibDate);
        params.put("trackingNo", trackingNo);
        params.put("customerId", customerId);
        if (trackingNos != null && !trackingNos.isEmpty()) {
            params.put("trackingNos", trackingNos);
        }
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<Map<String, Object>> result = returnService.getReturnList(params);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询退货详情
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxReturnList> getReturnDetail(@RequestParam Long id) {
        LxReturnList returnList = returnService.getReturnById(id);
        if (returnList != null) {
            return Result.success(returnList);
        }
        return Result.error("退货记录不存在");
    }
    
    /**
     * 创建退货
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxReturnList> createReturn(@RequestBody LxReturnList returnList) {
        try {
            LxReturnList result = returnService.createReturn(returnList);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建退货失败", e);
            return Result.error("创建退货失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新退货
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateReturn(@RequestBody LxReturnList returnList) {
        try {
            boolean success = returnService.updateReturn(returnList);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新退货失败", e);
            return Result.error("更新退货失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除退货
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteReturn(@RequestParam Long id) {
        try {
            boolean success = returnService.deleteReturn(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除退货失败", e);
            return Result.error("删除退货失败：" + e.getMessage());
        }
    }
}

